﻿using System;
using System.Windows.Forms;
using System.Xml;
using KTDSMS;

namespace KTD_XSMS
{
    public partial class EditPhone : Form
    {
        XmlDocument Doc = new XmlDocument();
        public string phoneNo, name;
        public EditPhone()
        {
            InitializeComponent();
        }

        public void Message(string mess)
        {
            MessageBox.Show(mess, "Thông báo hệ thống", MessageBoxButtons.OK);
        }
 
        private void btnSave_Click(object sender, EventArgs e)
        {
            string text = (string)null;
            if (string.IsNullOrEmpty(this.txtName.Text.Trim()))
                text = "Please enter Name";

            if (string.IsNullOrEmpty(text))
            {
                if (string.IsNullOrEmpty(this.txtPhoneNo.Text.Trim()))
                    text = "Please enter PhoneNo";
            }
            if (Unity.Incontent(txtPhoneNo.Text.Trim()) == "")
            {
                Unity.Message("Format number incorect");
                return;
            }
            if (string.IsNullOrEmpty(text))
            {
                // enter code here
                Doc.Load(Application.StartupPath + "\\PhoneTable.xml");
                XmlNode node = Doc.SelectSingleNode("/PhoneTables/PhoneTable[PhoneNo='" + this.phoneNo + "']");
                if (node != null)
                {
                    node.ChildNodes[0].InnerText = txtName.Text.Trim();
                    node.ChildNodes[1].InnerText = txtPhoneNo.Text.Trim();
                    Message("Edit Success");
                }

                Doc.Save(Application.StartupPath + "\\PhoneTable.xml");
                base.Dispose();
            }
            else
            {
                MessageBox.Show(text);
            }
        }

        private void EditPhone_Load(object sender, EventArgs e)
        {
            txtName.Text = this.name;
            txtPhoneNo.Text = this.phoneNo;
        }

        private void btnCancel_Click(object sender, EventArgs e)
        {
            base.Dispose();
        }

        private void txtPhoneNo_KeyDown(object sender, KeyEventArgs e)
        {
            if (e.KeyCode == Keys.Enter)
            {
                txtName.Focus();
            }
        }

        private void txtName_KeyDown(object sender, KeyEventArgs e)
        {
            if (e.KeyCode == Keys.Enter)
            {
                btnSave.Focus();
            }
        }
    }
}
